package com.qingcheng.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.order.WxPayService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.util.Map;

public class Test implements MessageListener {

    @Reference
    private OrderService orderService;

    @Reference
    private WxPayService wxPayService;


    public void onMessage(Message message) {
        String orderId=new String(message.getBody());
        System.out.println(orderId);
        Order order = orderService.findById(orderId);
        Map<String, String> queryMap = wxPayService.queryPayStatus(order.getId());
        if (order != null) {
            //1.先查询微信接口状态是否成功 支付成功 trade_state SUCCESS
            if (!"SUCCESS".equals(queryMap.get("trade_state"))) {
                //1.1关闭微信接口
                //1.2关闭订单
                wxPayService.closeNative(order.getId());
                orderService.closeOrder(order.getId());
                System.out.println("执行了");
              //  return;
            } else {
                //支付成功  修改订单状态 记录订单日志 transaction_id
                String transactionId = queryMap.get("transaction_id");
                orderService.updatePayStatus(order.getId(), transactionId);
              //  return;
            }
        }

    }

}
